package com.cskaoyan.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.text.SimpleDateFormat;

/**
 * @Description
 * @Author zoe ling
 * @Date 2024/5/25
 */
public class JacksonUtil {
    private static ObjectMapper objectMapper=new ObjectMapper();
    static {
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    }
    //响应
    public static String write(Object instance){
        try {
            return objectMapper.writeValueAsString(instance);

        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
    //请求
    public static <T> T read(String jasonStr,Class<T> clazz){
        try {
            return objectMapper.readValue(jasonStr,clazz);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
